import axiosInstance from "../../plugins/axios";
import { ElMessage } from "element-plus";
import { TaskDTO } from "../../type/Task";
import { ProjectDTOs } from "../../type/Project";
import { Role } from "../../type/Role";
import { User } from "../../type/User";
export const getTaskInfo = async (pageNum: number, pageSize: number) => {
  const res = await axiosInstance.post(
    `/tasks/page?pageNum=${pageNum}&pageSize=${pageSize}`
  );
  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取租户信息失败");
    throw new Error(res.data.message || "获取租户信息失败");
  }
};

export const searchTaskInfo = async (
  pageNum: number,
  pageSize: number,
  search: any
) => {
  const res = await axiosInstance.post("/tasks/search", {
    pageNum,
    pageSize,
    search,
  });
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "搜索任务信息失败");
    throw new Error(res.data.message || "搜索用户信息失败");
  }
};

export const findUserTaskByProjectId = async (
  taskId: string
): Promise<TaskDTO> => {
  const res = await axiosInstance.post("/tasks/findUserTaskByProjectId", {
    taskId,
  });

  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取任务信息失败");
    throw new Error(res.data.message || "获取任务信息失败");
  }
};

export const getUsersByType = async (
  taskDTO: TaskDTO
): Promise<ProjectDTOs> => {
  const res = await axiosInstance.post("/projects/getUsersByType", taskDTO);

  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取项目信息失败");
    throw new Error(res.data.message || "获取项目信息失败");
  }
};

export const findUserByProjectTask = async (
  projectId: string,
  taskType: string
): Promise<ProjectDTOs> => {
  const res = await axiosInstance.post("/projects/findUserByProjectTask", {
    projectId,
    taskType,
  });

  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取项目信息失败");
    throw new Error(res.data.message || "获取项目信息失败");
  }
};

export const updateProgress = async (taskDTO: TaskDTO) => {
  const res = await axiosInstance.post("/tasks/updateProgress", taskDTO);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "更新任务进度失败");
    throw new Error(res.data.message || "更新任务进度失败");
  }
};

export const inviteUserInEdit = async (
  taskDTO: TaskDTO,
  role: Role,
  user: User
) => {
  const res = await axiosInstance.post("/tasks/inviteUserInEdit", {
    taskDTO,
    role,
    user,
  });
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "邀请用户失败");
    throw new Error(res.data.message || "邀请用户失败");
  }
};
export const updateTaskForm = async (taskDTO: TaskDTO) => {
  const res = await axiosInstance.post("/tasks/update", taskDTO);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "更新任务信息失败");
    throw new Error(res.data.message || "更新任务信息失败");
  }
};
export const removeUserTask = async (taskDTO: TaskDTO, userId: string) => {
  const res = await axiosInstance.post("/tasks/removeUserTask", {
    taskDTO,
    userId,
  });
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "移除用户失败");
    throw new Error(res.data.message || "移除用户失败");
  }
};
export const deleteTaskUser = async (taskDTO: TaskDTO) => {
  const res = await axiosInstance.post("/tasks/delete", taskDTO);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "删除任务信息失败");
    throw new Error(res.data.message || "删除任务信息失败");
  }
};

export const getPreTaskInfo = async (taskDTO: TaskDTO) => {
  const res = await axiosInstance.post("/tasks/getPreTask", taskDTO);
  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取前置任务信息失败");
    throw new Error(res.data.message || "获取前置任务信息失败");
  }
};
export const findByPreTaskId = async (taskDTO: TaskDTO) => {
  const res = await axiosInstance.post("/tasks/findById", taskDTO);
  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取任务信息失败");
    throw new Error(res.data.message || "获取任务信息失败");
  }
};

export const exportTaskInfo = async () => {
  axiosInstance
    .get("/tasks/export", {
      responseType: "blob", // 重要
    })
    .then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement("a");
      link.href = url;
      const contentDisposition = response.headers["content-disposition"];
      let fileName = "下载文件.xlsx";
      if (contentDisposition) {
        const fileNameMatch = contentDisposition.match(
          /filename\*=UTF-8''(.+)/
        );
        if (fileNameMatch.length === 2) {
          fileName = decodeURIComponent(fileNameMatch[1]);
        }
      }
      link.setAttribute("download", fileName);
      document.body.appendChild(link);
      link.click();
      link.remove();
    })
    .catch((error) => {
      ElMessage.error("下载失败，请重试");
      console.error("下载失败:", error);
    });
};